﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RssMVC4.ServiceReference;
using System.Xml;
using System.ServiceModel.Syndication;
using System.Web.Security;

namespace RssMVC4.Controllers
{
    [Authorize]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            try
            {
                using (ServiceRssClient client = new ServiceRssClient())
                {
                    var obj = Session["Password"];
                    if (obj == null)
                    {
                        FormsAuthentication.SignOut();
                        return Redirect("~/account/login");
                    }
                    LoginModel login = new LoginModel { UserName = HttpContext.User.Identity.Name, Password = Session["Password"].ToString() };
                    var flux = client.GetFlux(login);

                    return View(flux.ToList());
                }
            }
            catch(Exception ex)
            {
                HandleErrorInfo error = new HandleErrorInfo(ex, "Home", "ShowRss");
                ViewBag.Error = ex.Message;
                return PartialView("Error", error);
            }
          
        }

        public ActionResult ShowRss(Flux flux)
        {
            if (ModelState.IsValid)
            {
                using (ServiceRssClient client = new ServiceRssClient())
                {
                    try
                    {
                        LoginModel login = new LoginModel { UserName = HttpContext.User.Identity.Name, Password = Session["Password"].ToString() };
                        var rss = client.GetRss(login, flux);
                        return View(rss.ToList());
                    }
                    catch (Exception ex)
                    {
                        HandleErrorInfo error = new HandleErrorInfo(ex, "Home", "ShowRss");
                        ViewBag.Error = ex.Message;
                        return PartialView("Error", error);
                    }
                }
            }
            ModelState.AddModelError("", "Model is not Valid");
            return RedirectToAction("Index", "Home");
        }

        public ActionResult EditRss(Flux flux)
        {
            return View(flux);
        }

        [HttpPost]
        public ActionResult EditRss(EditFlux flux)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    using (ServiceRssClient client = new ServiceRssClient())
                    {

                        LoginModel login = new LoginModel { UserName = HttpContext.User.Identity.Name, Password = Session["Password"].ToString() };

                        FluxStatus success = client.EditRss(login, flux);
                        if (success != FluxStatus.Success)
                        {
                            ModelState.AddModelError("", success.ToString());
                        }
                        return RedirectToAction("Index", "Home");
                    }
                }
                catch (Exception ex)
                {
                    HandleErrorInfo error = new HandleErrorInfo(ex, "Home", "ShowRss");
                    ViewBag.Error = ex.Message;
                    return PartialView("Error", error);
                }
            }
            return View(flux);
        }

        public ActionResult DeleteRss(int ID)
        {
            try
            {
                using (ServiceRssClient client = new ServiceRssClient())
                {
                    DeleteRss deleteRss = new DeleteRss()
                    {
                        IdFlux = ID,
                        UserName = HttpContext.User.Identity.Name
                    };
                    LoginModel login = new LoginModel { UserName = HttpContext.User.Identity.Name, Password = Session["Password"].ToString() };
                    FluxStatus success = client.RemoveRss(login, deleteRss);
                    if (success != FluxStatus.Success)
                    {
                        ModelState.AddModelError("", success.ToString());
                    }
                    return RedirectToAction("Index", "Home");
                }
            }
            catch (Exception ex)
            {
                HandleErrorInfo error = new HandleErrorInfo(ex, "Home", "ShowRss");
                ViewBag.Error = ex.Message;
                return PartialView("Error", error);
            }
        }

        public ActionResult CreatRss()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreatRss(RegisterRss newRss)
        {
            newRss.UserName = HttpContext.User.Identity.Name;
            if (ModelState.IsValid)
            {
                try
                {
                    using (ServiceRssClient client = new ServiceRssClient())
                    {
                        LoginModel login = new LoginModel { UserName = HttpContext.User.Identity.Name, Password = Session["Password"].ToString() };
                        FluxStatus success = client.AddRss(login, newRss);
                        if (success != FluxStatus.Success)
                        {
                            ModelState.AddModelError("", success.ToString());
                            return View(newRss);
                        }
                        return RedirectToAction("Index", "Home");
                    }
                }
                catch (Exception ex)
                {
                    HandleErrorInfo error = new HandleErrorInfo(ex, "Home", "ShowRss");
                    ViewBag.Error = ex.Message;
                    return PartialView("Error", error);
                }
            }
            return View(newRss);
        }
    }
}
